﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DatumTest2
{
    class Program
    {
        static string spamTestFolderPath = "spam-test";
        static string nonspamTestFolderPath = "nonspam-test";
        static int n = 0;
        static void Main(string[] args)
        {
            DatumboxAPI DatumboxAPI = new DatumboxAPI("a10145b49db352ba8bef9591a8d58cdd");
            int correct = 0, total = 0;

            List<string> spamList = new List<string>();
            List<string> nonspamList = new List<string>();
            foreach (string file in Directory.EnumerateFiles(spamTestFolderPath, "*.txt"))
            {
                spamList.Add(File.ReadAllText(file));
            }
            foreach (string file in Directory.EnumerateFiles(nonspamTestFolderPath, "*.txt"))
            {
                nonspamList.Add(File.ReadAllText(file));
            }

            foreach (string spamText in spamList)
            {
                var json = DatumboxAPI.SpamDetection(spamText);

                var obj = JObject.Parse(json);
                var result = (JToken)obj.GetValue("output");
                Console.WriteLine(n++);
                if ((String)result["result"] == "spam")
                {
                    correct++;
                }
                total++;
            }

            foreach (string nonspamText in nonspamList)
            {
                var json = DatumboxAPI.SpamDetection(nonspamText);

                var obj = JObject.Parse(json);
                var result = (JToken)obj.GetValue("output");
                Console.WriteLine(n++);
                if ((String)result["result"] == "nonspam")
                {
                    correct++;
                }
                total++;
            }
            Console.WriteLine(correct + "  " + total);
        }
    }
}
